package com.liveneo.system.base.utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

public class RequestUtil {

    /**
     * 获得用户远程地址
     * 
     * @param request
     * @return
     */
    public static String getRemoteAddr(HttpServletRequest request){
        String remoteAddr = request.getHeader("X-Real-IP");
        if (StringUtils.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("X-Forwarded-For");
        }else if (StringUtils.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("Proxy-Client-IP");
        }else if (StringUtils.isNotBlank(remoteAddr)) {
            remoteAddr = request.getHeader("WL-Proxy-Client-IP");
        }
        return remoteAddr != null ? remoteAddr : request.getRemoteAddr();
    }
    
    /**
     * 转换请求的所有参数为字符串
     * 
     * @param request
     * @return
     */
    public static String convertParamtersToStr(HttpServletRequest request){
    	Map<String, String[]> paramterMap = request.getParameterMap();
    	String strParamters = "";
    	
    	if(paramterMap.size()>0){
    		Map<String, List<String>> paramterListMap = new HashMap<String, List<String>>();
    		for(Entry<String, String[]> paramter : paramterMap.entrySet()){
    			paramterListMap.put(paramter.getKey(), Arrays.asList(paramter.getValue()));
    		}
    		
    		strParamters = paramterListMap.toString();
    	}
    	
    	return strParamters;
    }
}
